package com.sfdk.pools
{
import com.sfdk.graphics.engine.animation.AnimatedBitmap;

import flash.utils.Dictionary;

public class AnimatedBitmapPool
	{
		private static var _instance : AnimatedBitmapPool;
		
		private var _pool : Dictionary;
		private var _totalSize : int;
		
		public function AnimatedBitmapPool()
		{
			_pool = new Dictionary();
			_totalSize = 0;
			_instance = this;	
		}
		
		public static function get instance() : AnimatedBitmapPool
		{
			return _instance;
		}
		
		public static function getFromPool(name : String) : AnimatedBitmap
		{
			return _instance.getFromPool(name);
		}
		
		/**
		 * Ajoute les spritesheet à la map
		 * Renvoi :
		 * 			-1 si la spritesheet existe deja
		 * 			1 si tout est ok
		 **/
		public function addToPool(spritesheet : AnimatedBitmap, name : String) : int
		{
			if(_pool[name] != null)
				return -1;
			
			_pool[name] = spritesheet;
			return 1;				
		}
		
		public function removeFromPool(name : String) : void
		{
			_pool[name] = null;
		}
		
		public function getFromPool(name : String) : AnimatedBitmap
		{
			var animatedBitmap : AnimatedBitmap = _pool[name] as AnimatedBitmap;
			if(animatedBitmap == null)
			{
				animatedBitmap = new AnimatedBitmap(name);
				_pool[name] = animatedBitmap;
			}
			return animatedBitmap;
		}
		
		public function isInPool(name : String) : Boolean
		{
			return (_pool[name] != null);
		}
	}
}